<?php
namespace app\xiangmu\controller;
class Userxiangmu extends Base
{
		 //列表
	   public function lists() {
        if (is_post()) {
            $d = $this->post;
            $map = [];
						if($d['keyword']){ $map[] = ['and','b.username','like',"%{$d['keyword']}%"]; }
            if($d['xiangmu_id']){ $map[] = ['and','a.xiangmu_id','=',$d['xiangmu_id']]; }
            if($d['min_time']) $map[] = ['and','a.end_time','>=',strtotime($d['min_time'])];
            if($d['max_time']) $map[] = ['and','a.end_time','<=',strtotime($d['max_time'])];
            $w = where($map);
            $rs = $this->db->query("select a.*,b.username,c.title from user_xiangmu a left join user b on a.user_id=b.id left join xiangmu c on a.xiangmu_id=c.id $w[0] order by id desc" . limit($d['page'], $d['limit']), $w[1], '', true);
            if ($rs) {
                foreach ($rs[0] as $key => $val) {
                  $rs[0][$key]['status'] = c("type.status")[$val['status']];
									$rs[0][$key]['end_time'] = date('Y-m-d H:i', $val['end_time']);
                  $rs[0][$key]['create_time'] = date('Y-m-d H:i', $val['create_time']);
                }
                suc($rs[0], $rs[1]);
            } else {
                suc([], 0);
            }
        }
				$xiangmu = $this->db->query("select id, title from xiangmu");
        $this->fetch(['xiangmu'=>$xiangmu],'','layadmin');
    }

		//编辑
		public function edit(){
			 if (is_post()) {
					 $d = $this->post;
					 $rule = [
							 'end_time'=>'require',
					 ];
					 $msg = [];
					 check($rule,$msg,$d);
					 $d['update_time'] = time();
					 $d['end_time']    = strtotime($d['end_time']);
					 $this->db->update('user_xiangmu',$d);
					 cookie('js_msg', '修改成功');
					 cookie('js_function', 'preload');
					 suc();
			 }
			 $map = [['and','a.id','=',$this->get('id')]];
			 $w  = where($map);
			 $info = $this->db->query("select a.*,b.username,c.title from user_xiangmu a left join user b on a.user_id=b.id left join xiangmu c on a.xiangmu_id=c.id $w[0]", $w[1],1);
			 $this->fetch(['info'=>$info],'','layno');
		}
}
